
package com.ifcar.app.model;

import com.ifcar.app.db.DBUtils;
import com.ifcar.app.db.NewsSQLiteOpenHelper;
import com.ifcar.app.util.StringUtil;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Attitude {
    public static void insertTopicListToDB(SQLiteDatabase db, List<Map<String, Object>> topicData,
            String columnID, String more) {
        String docid = "";
        String imgsrc = "";
        String digest = "";
        String url = "";
        String replyCount = "";
        String title = "";
        String ptime = "";
        String subtitle = "";
        String hasimg = "0";
        String specialid = "";
        String sepcialindex = "";
        String sepcialtname = "";

        db.beginTransaction();
        Cursor cursor = null;
        try {
            for (int i = 0; i < topicData.size(); i++) {
                try {
                    if (((Map<String, Object>) topicData.get(i)).get(StringUtil.TOPIC_DOCID) != null) {
                        docid = StringUtil.GetString(((Map<String, Object>) topicData.get(i))
                                .get(StringUtil.TOPIC_DOCID));
                        String sql = "select * from " + NewsSQLiteOpenHelper.NEWS_LIST_TABLE
                                + " where " + NewsSQLiteOpenHelper.NEWS_DOC_ID + "=? and "
                                + NewsSQLiteOpenHelper.NEWS_COL_ID + "='" + columnID + "'";
                        ;
                        cursor = db.rawQuery(sql, new String[] {
                            docid
                        });

                        if (cursor != null
                                && cursor.getCount() > 0
                                && i != 0
                                && cursor.moveToFirst()
                                && cursor.getString(
                                        cursor.getColumnIndex(NewsSQLiteOpenHelper.NEWS_IS_FIRST))
                                        .equals("1")) {
                            ContentValues initialValues = new ContentValues();
                            initialValues.put(NewsSQLiteOpenHelper.NEWS_IS_FIRST, "0");
                            db.update(NewsSQLiteOpenHelper.NEWS_LIST_TABLE, initialValues,
                                    "news_doc_id=?", new String[] {
                                        docid
                                    });
                        }

                        if (cursor == null || cursor.getCount() == 0) {
                            if (((Map<String, Object>) topicData.get(i))
                                    .get(StringUtil.TOPIC_IMGSRC) != null) {
                                imgsrc = StringUtil.GetString(((Map<String, Object>) topicData.get(i)).get(
                                        StringUtil.TOPIC_IMGSRC));
                            }
                            if (((Map<String, Object>) topicData.get(i))
                                    .get(StringUtil.TOPIC_DIGEST) != null) {
                                digest = StringUtil.GetString(((Map<String, Object>) topicData.get(i)).get(
                                        StringUtil.TOPIC_DIGEST));
                            }
                            if (((Map<String, Object>) topicData.get(i)).get(StringUtil.TOPIC_URL) != null) {
                                url = StringUtil.GetString(((Map<String, Object>) topicData.get(i)).get(
                                        StringUtil.TOPIC_URL));
                            }
                            if (((Map<String, Object>) topicData.get(i))
                                    .get(StringUtil.TOPIC_TITLE) != null) {
                                title = StringUtil.GetString(((Map<String, Object>) topicData.get(i)).get(
                                        StringUtil.TOPIC_TITLE));
                            }
                            if (((Map<String, Object>) topicData.get(i))
                                    .get(StringUtil.TOPIC_PTIME) != null) {
                                ptime = StringUtil.GetString(((Map<String, Object>) topicData.get(i)).get(
                                        StringUtil.TOPIC_PTIME));
                            }
                            if (((Map<String, Object>) topicData.get(i))
                                    .get(StringUtil.TOPIC_SUBTITLE) != null) {
                                subtitle = StringUtil.GetString(((Map<String, Object>) topicData.get(i)).get(
                                        StringUtil.TOPIC_SUBTITLE));
                            }
                            if (((Map<String, Object>) topicData.get(i))
                                    .get(StringUtil.TOPIC_SPECIAL_ID) != null) {
                                specialid = StringUtil.GetString(((Map<String, Object>) topicData.get(i)).get(
                                        StringUtil.TOPIC_SPECIAL_ID));
                            }

                            if (i == 0 && !more.equals("more")) {
                                DBUtils.insertDataToNewsListTable(db, columnID, docid, imgsrc,
                                        digest, replyCount, url, title, ptime, subtitle, "1", "N",
                                        hasimg, specialid, sepcialindex, sepcialtname, "");
                            } else {
                                DBUtils.insertDataToNewsListTable(db, columnID, docid, imgsrc,
                                        digest, replyCount, url, title, ptime, subtitle, "0", "N",
                                        hasimg, specialid, sepcialindex, sepcialtname, "");
                            }

                        }
                        if (null != cursor) {
                            cursor.close();
                        }
                    }

                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    if (null != cursor) {
                        cursor.close();
                    }
                }
            }
            db.setTransactionSuccessful();
        } catch (Exception e) {
            // 当数据库操作出现错误时，需要捕获异常，结束事务
            db.endTransaction();
        }
        db.endTransaction();

    }

    public static List<Map<String, Object>> getTopicListFromDB(SQLiteDatabase db, String columnID) {
        List<Map<String, Object>> topicData = new ArrayList<Map<String, Object>>();

        String sql = "select * from " + NewsSQLiteOpenHelper.NEWS_LIST_TABLE + " where "
                + NewsSQLiteOpenHelper.NEWS_COL_ID + "=? and " + NewsSQLiteOpenHelper.NEWS_IS_FIRST
                + "=?";
        Cursor cursor = db.rawQuery(sql, new String[] {
                columnID, "1"
        });

        if (cursor != null && cursor.getCount() != 0 && cursor.moveToFirst()) {

            Map<String, Object> map = null;

            try {
                map = new HashMap<String, Object>();
                map.put(StringUtil.TOPIC_DOCID, cursor.getString(cursor
                        .getColumnIndex(NewsSQLiteOpenHelper.NEWS_DOC_ID)));
                map.put(StringUtil.TOPIC_IMGSRC, cursor.getString(cursor
                        .getColumnIndex(NewsSQLiteOpenHelper.NEWS_IMG_SRC)));
                map.put(StringUtil.TOPIC_DIGEST, cursor.getString(cursor
                        .getColumnIndex(NewsSQLiteOpenHelper.NEWS_DIGEST)));
                map.put(StringUtil.TOPIC_SUBTITLE, cursor.getString(cursor
                        .getColumnIndex(NewsSQLiteOpenHelper.NEWS_SUBTITLE)));
                map.put(StringUtil.TOPIC_URL, cursor.getString(cursor
                        .getColumnIndex(NewsSQLiteOpenHelper.NEWS_URL)));
                map.put(StringUtil.TOPIC_TITLE, cursor.getString(cursor
                        .getColumnIndex(NewsSQLiteOpenHelper.NEWS_TITLE)));
                if (!cursor.getString(cursor.getColumnIndex(NewsSQLiteOpenHelper.NEWS_PTIME))
                        .equals("")) {
                    map.put(StringUtil.TOPIC_PTIME, cursor.getString(cursor
                            .getColumnIndex(NewsSQLiteOpenHelper.NEWS_PTIME)));
                }

                topicData.add(map);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                if (null != cursor) {
                    cursor.close();
                }
            }
            cursor.close();
        }
        
        if (cursor != null && !cursor.isClosed()) {
            cursor.close();
        }

        sql = "select * from " + NewsSQLiteOpenHelper.NEWS_LIST_TABLE + " where "
                + NewsSQLiteOpenHelper.NEWS_COL_ID + "=? and " + NewsSQLiteOpenHelper.NEWS_IS_FIRST
                + "=? order by news_ptime desc limit 0,20";
        cursor = db.rawQuery(sql, new String[] {
                columnID, "0"
        });

        if (cursor != null && cursor.getCount() != 0) {

            Map<String, Object> map = null;
            for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
                try {
                    map = new HashMap<String, Object>();
                    map.put(StringUtil.TOPIC_DOCID, cursor.getString(cursor
                            .getColumnIndex(NewsSQLiteOpenHelper.NEWS_DOC_ID)));
                    map.put(StringUtil.TOPIC_IMGSRC, cursor.getString(cursor
                            .getColumnIndex(NewsSQLiteOpenHelper.NEWS_IMG_SRC)));
                    map.put(StringUtil.TOPIC_DIGEST, cursor.getString(cursor
                            .getColumnIndex(NewsSQLiteOpenHelper.NEWS_DIGEST)));
                    map.put(StringUtil.TOPIC_SUBTITLE, cursor.getString(cursor
                            .getColumnIndex(NewsSQLiteOpenHelper.NEWS_SUBTITLE)));
                    map.put(StringUtil.TOPIC_URL, cursor.getString(cursor
                            .getColumnIndex(NewsSQLiteOpenHelper.NEWS_URL)));
                    map.put(StringUtil.TOPIC_TITLE, cursor.getString(cursor
                            .getColumnIndex(NewsSQLiteOpenHelper.NEWS_TITLE)));
                    if (!cursor.getString(cursor.getColumnIndex(NewsSQLiteOpenHelper.NEWS_PTIME))
                            .equals("")) {
                        map.put(StringUtil.TOPIC_PTIME, cursor.getString(cursor
                                .getColumnIndex(NewsSQLiteOpenHelper.NEWS_PTIME)));
                    }

                    topicData.add(map);
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    if (null != cursor) {
                        cursor.close();
                    }
                }
            }
            cursor.close();
        }
        if (cursor != null && !cursor.isClosed()) {
            cursor.close();
        }
        return topicData;
    }
}
